package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@EnableKnife4j
@Tag(name="02-车辆管理模块")
@Validated
@RestController
@RequestMapping("/v1/vehicle")
public class VehicleController {

    @Autowired
    private VehicleMapper vehicleMapper;



    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, HttpSession  session) {
        log.debug("开始处理添加车辆功能:");
        log.debug("添加车辆信息："+vehicleAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录校验已经转移到监控器
        if(vehicleAddParam==null){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        return vehicleMapper.insertVehicle(vehicle) > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @Operation(summary = "车辆列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("list")
    @Parameters(value = {
            @Parameter(name = "brand", description = "车辆品牌"),
            @Parameter(name = "license", description = "车辆车牌"),
            @Parameter(name = "vehicleListQuery", description =  "车辆列表查询参数对象",hidden = true)
    })
    public JsonResult list(VehicleListQuery vehicleListQuery, HttpSession session) {
        log.debug("查询参数：" + vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录校验已经转移到监控器
        log.debug("userVO="+userVO);
        List<VehicleListVO> vehicleListVOS = vehicleMapper.selectVehicleList(vehicleListQuery, userVO.getId());
        log.debug("查询结果：" + vehicleListVOS);
        return JsonResult.ok(vehicleListVOS);
    }


    @PostMapping("del/{id}")
    @Operation(summary="删除车辆信息")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehicleById(@Range(min=1L,max=500L,message = "车辆编号必须在1-500之间")
                                            @PathVariable Long id,
                                                          HttpSession session) {

        if(id < 10){
            throw new IllegalArgumentException("id小于0");
        }
        log.debug("id="+id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录校验已经转移到监控器
        vehicleMapper.deleteVehicleById(id,userVO.getId());
        return JsonResult.ok();
    }
























}
